var isLogOut = false;
var rootpath;
var basePath;
window.onbeforeunload = function(e) {
	if($("#myTab>li[tagid]").length > 0) {
		return "存在未关闭的选项卡，离开此页可能会导致未保存数据丢失，您确定离开此页吗？"
	}
};
$(function() {
	InitLeftMenu();
	InitTab();
	$(window).triggerHandler("resize.navbar");
	resize_sidebar();
	resizetabs();
	resize_dhbar();
});
var hidePageLoad = function() {
	$("._pageLoad").addClass("hidden")
};
var logout = function() {
	$.messager.confirm("系统提示", "您确定要退出本次登录吗?", function(e) {
		if(e) {
			isLogOut = true;
		}
	})
};
var resizetabs = function() {
	var e = $(window).width();
	var t = $(window).height();
	var a = $("#myTabContent").offset();
	$("#myTabContent").find("div.tab-pane.fade.active").height(t - a.top - 4).width(e - a.left);
	$("#myTabContent iframe.iframe_tabpage").height(t - a.top - 4.8)
};
var resize_sidebar = function() {
	var e = $(window).height();
	if(!$.browser.useapp) {
		if($("#sidebar").height() + $("#sidebar").offset().top > e) {
			$("#sidebar").addClass("mx-sidebar");
			$("#sidebar").removeClass("compact")
		}
	} else {
		$("#sidebar").removeClass("compact");
		$("#sidebar li.hover").removeClass("hover")
	}
};

function InitLeftMenu() {
	SetMenusNew()
}

function SetMenusNew() {
	$.each(menus, function(e, t) {
		if(t.isRight==1){
			var a = $(getMenuBtn(t));
			a.appendTo("#menubar")
		}
	});
	$("#sidebar li").addClass("hover");
	$("li.menuclick>a", "#menubar").on("click.menuclick", function() {
		$(".hover-show.hover-shown", "#menubar").removeClass("hover-show hover-shown");
		var e = $(this);
		if(!e.attr("url")) {
			return
		}
		var t = e.attr("url");
		var a = e.attr("objid");
		var n = e.text();
		var i = e.attr("icon");
		if(i == "null") {
			i = ""
		}
		var r = e.attr("tagid");
		hidemenu();
		setTimeout(addTab(r, n, t, i, a), 100)
	});
}
var getMenuBtn = function(e) {
	var t = "";
	var a = "";
	if(e.children != null && e.children.length > 0) {
		t = "dropdown-toggle";
		a = '<b class="arrow fa fa-angle-right"></b>'
	}
	var n = '<li name="PMenu"><a name="item" href="###" class="' + t + '" tagid="' + e.id + '"> <i class="menu-icon fa ' + e.iconCls + '"></i><span class="menu-text">' + e.text + " </span>" + a + '</a><b class="arrow"></b>';
	if(e.children) {
		n = n + '	<ul class="submenu">';
		$.each(e.children, function(e, t) {
			n = n + getMenuStr(t)
		});
		n = n + "</ul>"
	}
	n = n + "</li>";
	return n
};
var getMenuStr = function(e) {
	var t = "";
	var a = "";
	var n = "";
	var i = "";
	if(e.children != null && e.children.length > 0) {
		t = "dropdown-toggle";
		a = '<b class="arrow fa fa-angle-right"></b>'
	} else {
		n = 'url="' + rootpath+"/"+e.url + '" objid ="' + e.id + '" icon="' + e.iconCls + '"  ';
		if(e.url) {
			i = "menuclick"
		}
	}
	var r = '<li  class="' + i + '"><a name="item" href="###" class="' + t + '" ' + n + ' tagid="' + e.id + '"> <i class="menu-icon fa ' + e.iconCls + '"></i>' + e.text + " " + a + '</a><b class="arrow"></b>';
	if(e.children && e.children.length > 0) {
		r = r + '	<ul class="submenu">';
		$.each(e.children, function(e, t) {
			r = r + getMenuStr(t)
		});
		r = r + "</ul>"
	}
	r = r + "</li>";
	return r
};
var activeMenu = function(e) {
	$("li[name='PMenu']").removeClass("active");
	$('a[tagid="' + e + '"][name="item"]', "#menubar").closest("li[name='PMenu']").addClass("active")
};
var getMenuByID = function(e) {};

function InitTab() {
	$("a", "#myTab>li:first").tab("show");
	$("#myTab").tabdrop({
		text: '更多<i class="ace-icon fa fa-caret-down bigger-110 width-auto"></i>'
	}).on("shown.bs.tab", function(e) {
		var t = $($(e.target).attr("href"));
		$("iframe", t).each(function() {
			if(this.contentWindow && this.contentWindow.new_resize) {
				this.contentWindow.new_resize()
			}
			if(this.contentWindow && this.contentWindow.smallscreen_reset) {
				this.contentWindow.smallscreen_reset()
			}
		});
		resize_dhbar()
	})
}

function RefThisTab() {
	var e = $($("#myTab").find("li.active"));
	$("#tab-" + e.attr("tagid")).find("._pageLoad").removeClass("hidden");
	var t = $("#ifarme-" + e.attr("tagid"));
	t.attr("src", t.attr("src"))
}

function CloseThisTab() {
	var e = $($("#myTab").find("li.active"));
	closeTab(e)
}

function CloseAllTab(e) {
	var t = $("#myTab").find("li");
	$.each(t, function(t, a) {
		if(!$(a).hasClass("active")) {
			if($(a).attr("tagid") != null) {
				closeTab($(a), true)
			}
		} else {
			if(e != null) {
				closeTab($(a), true)
			}
		}
	})
}

function addTabByIDWithParams(e, t) {
	var a = $('a[name="item"][tagid="' + e + '"]');
	if(a.length <= 0) {
		return
	}
	var n = a.text();
	var i = "/" + a.attr("url") + "?PageConnect=true";
	if(!!t, t.length > 0) {
		$.each(t, function(e, t) {
			i = i + "&" + (t.key + "=") + encodeURI(t.val)
		})
	}
	var r = a.attr("icon");
	var o = a.attr("objid");
	var s = $('[tagid="' + e + '"]', "#myTab");
	if(s.length > 0) {
		$.messager.confirm("", "您需要联查至的页面已打开,重新打开将会刷新该页面。是否确定要联查至该页面？", function(t) {
			if(t) {
				$("a", s).tab("show");
				activeMenu(e);
				$("#tab-" + e).find("._pageLoad").removeClass("hidden");
				$("#ifarme-" + e).attr("src", i)
			}
		});
		return
	}
	addTab(e, n, i, r, o)
}

function addTabByID(e) {
	var t = $('a[name="item"][tagid="' + e + '"]');
	if(t.length <= 0) {
		return
	}
	var a = t.text();
	var n = "/" + t.attr("url") + "?FastOpen=true";
	var i = t.attr("icon");
	var r = t.attr("objid");
	addTab(e, a, n, i, r)
}

function addTabByCountMark(e, t) {
	if(!e) return;
	var a = $('a[name="item"][tagid="' + e + '"]');
	if(a.length <= 0) {
		return
	}
	var n = a.text();
	var i = "/" + a.attr("url") + "?Type=" + t;
	var r = a.attr("icon");
	var o = a.attr("objid");
	var s = $('[tagid="' + e + '"]', "#myTab");
	if(s.length > 0) {
		$("a", s).tab("show");
		activeMenu(e);
		$("#ifarme-" + e).attr("src", i);
		return
	}
	addTab(e, n, i, r, o)
}

function addTabByIDAndParam(e, t) {
	var a = $('a[name="item"][tagid="' + e + '"]');
	if(a.length <= 0) {
		return
	}
	var n = a.text();
	var i = a.attr("icon");
	var r = "/" + a.attr("url") + t;
	var o = a.attr("objid");
	addTab(e, n, r, i, o)
}

function addTab(e, t, a, n, i) {
	var r = $('[tagid="' + e + '"]', "#myTab");
	if(r.length > 0) {
		$("a", r).tab("show");
		activeMenu(e);
		if(e == "printid") {
			var o = $("#ifarme-printid");
			o.attr("src", a)
		}
		return
	}
	var s = '<li tagid="' + e + '"><a data-toggle="tab" href="#tab-' + e + '" tagid="' + e + '"><i class="green ace-icon fa ' + n + '"></i>' + t + " </a></li>";
	r = $(s).bind("contextmenu", function(e) {
		var t = !e ? window.event : e;
		e.preventDefault();
		return false
	}).mouseup(function(e) {
		if(e.which == 3) {
			e.preventDefault();
			$("a", $(this)).tab("show");
			var t = $("div.RMclickmenu");
			var a = e.clientX;
			var n = e.clientY;
			if(n + t.height() > $(document).height()) {
				n = e.clientY - t.height()
			}
			t.css({
				left: a,
				top: n,
				display: "block"
			})
		}
	});
	$("#myTab").append(r);
	if(e != "menuFirstPage") {
		var l = '<i name="tabclose" class="ace-icon fa fa-remove fa-lg"></i>';
		l = $(l).appendTo($("a", r));
		l.bind("mouseenter", function() {
			$(this).addClass("red")
		}).bind("mouseleave", function() {
			$(this).removeClass("red")
		}).bind("click", function() {
			closeTabByA(this);
			return false
		})
	}
	var c = '<div id="tab-' + e + '" class="tab-pane fade" ><iframe id="ifarme-' + e + '" name="ifarme-' + e + '" objid = "' + i + '"  icon="' + n + '" title="' + t + '" class="iframe_tabpage"  frameborder="0"  src="' + a + '" style="width:100%;" helpurl></iframe><div style="background-color:white;position: absolute;top: 0px;bottom: 0px;right: 0px;left: 0px;text-align: center;min-height: 100%;" class="_pageLoad"><div style="position: absolute; left: 45%;top: 45%;"><div style=" background-image :url('+rootpath+'/static/common/platform/image/loading.gif);height: 32px;width: 32px;background-repeat:no-repeat;margin: auto;"></div><h4>正在为您加载页面...</h4></div></div></div>';
	var d = $(c).appendTo("#myTabContent");
	$("a", r).tab("show");
	$("iframe.iframe_tabpage").off("load.page").on("load.page", function() {
		//页面加载之前
	});
	$("#myTab").tabdrop("layout");
	activeMenu(e);
	resize_dhbar();
	resizetabs()
}
var showFirstPage = function() {
	var e = $("#tab-FirstPage");
	$("a", e).tab("show")
};

function closeTabByA(e) {
	var t = $(e).closest("li");
	closeTab(t)
}

function closeTab(e, t) {
	var a = e.attr("tagid");
	var n = $("#tab-" + a);
	var i = n.find("iframe");
	if(i && i.length > 0) {
		i[0].contentWindow.document.write("");
		if(i[0].contentWindow.document.clear) {
			i[0].contentWindow.document.clear()
		}
		i[0].contentWindow.close();
		i.find("*").remove();
		i.remove();
		if($.browser.msie) {
			CollectGarbage()
		}
	}
	n.remove();
	if(e.hasClass("active")) {
		if(e.prev("li").length > 0) {
			$("a", e.prev("li")).tab("show")
		} else if(e.next("li").length > 0) {
			$("a", e.next("li")).tab("show")
		}
		activeMenu(a)
	}
	e.remove();
	if(t) {} else {
		$("#myTab").tabdrop("layout")
	}
}
var fp_showFullScreen = function() {
	$("#myTabContent").addClass("mx_fullscreen");
	resizetabs()
};
var fp_hideFullScreen = function() {
	$("#myTabContent").removeClass("mx_fullscreen");
	resizetabs()
};

function getSearchCode(e) {
	var t = $("#myTab").find("li.active").attr("tagid");
	var a = $("#tab-" + t);
	var n = a.find("iframe");
	try {
		n[0].contentWindow.getSearchCode(e)
	} catch(i) {
		alert("error")
	}
}
var resize_dhbar = function() {
	var e = $($("#myTab").find("li.active:not(.tabdrop)"));
	var t = e.attr("tagid");
	var a = false;
	if(t && t != "menuFirstPage") {
		a = true
	} else {
		a = false
	}
	if($.browser.useapp) {
		$("body").addClass("mx-xs")
	} else {
		$("body").removeClass("mx-xs")
	}
	if($.browser.useapp && a) {
		$("#navbar").addClass("hidden");
		$("#myTab").closest(".widget-header").addClass("hidden");
		hidemenu()
	} else {
		$("#navbar").removeClass("hidden");
		$("#myTab").closest(".widget-header").removeClass("hidden")
	}
};
var returnFirstPage = function() {
	showFirstPage();
	resize_dhbar()
};
var hidemenu = function() {
	var e = $("button.menu-toggler");
	var t = $(e.attr("data-target"));
	if(t.length == 0) return;
	if(!e.hasClass("display")) {
		return
	}
	t.toggleClass("display");
	e.toggleClass("display");
	var a = ace.click_event + ".ace.autohide";
	var n = t.attr("data-auto-hide") === "true";
	if(e.hasClass("display")) {
		if(n) {
			$(document).on(a, function(n) {
				if(t.get(0) == n.target || $.contains(t.get(0), n.target)) {
					n.stopPropagation();
					return
				}
				t.removeClass("display");
				e.removeClass("display");
				$(document).off(a)
			})
		}
		if(t.attr("data-sidebar-scroll") == "true") t.ace_sidebar_scroll("reset")
	} else {}
};